#include <stdio.h>
#include <ctype.h>

int
val(char ch) {
	if (ch >= '0' && ch <= '9')
		return (ch - '0');
	else if (ch >= 'A' && ch <= 'Z')
		return (10 + ch - 'A');
	return 0;
}

int
main ()
{
	int base, out=0, negative=0;
	char in;
	//freopen("ptoten.in", "r", stdin);
	//freopen("ptoten.out", "w", stdout);
	scanf("%d", &base);
	while (scanf("%c", &in) == 1) {
		fprintf(stderr, "\t in = %c , out = %d , base = %d\n\n", in, out, base);
		//if (islower(in) || isspace(in) || ispunct(in))
			//break;
		if (in == '-')
			negative = 1;
		if (isupper(in) || isdigit(in)) {
			out = out*base + val(in);
		}
	}
	fprintf(stderr, "\t in = %c , out = %d , base = %d\n\n", in, out, base);
	//out /= base;
	if (negative)
		printf("%d\n", -out);
	else
		printf("%d\n", out);
	return 0;
}
